<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admincp extends Admin_Controller {
	protected $is_main_controller = true;
	protected $is_group_menu = true; // Config using grouped menu
	
	function __construct(){
		$action = $this->uri->segment(2);
		$this->is_special_action = !empty($action) && $action !='index';
		parent::__construct();
		$this->load->model('admincp_model','model');
	}
	
	function index(){
		$data['module'] = 'admincp';
		$this->template->write_view('content','index',$data);
		$this->template->render();
	}
	
	function menu(){
		$data['module'] = $this->uri->segment(2);
		$view = 'menu';
		if($this->is_group_menu){
			$module_group_list = $this->model->fetch('*',ADMIN_MODULE_GROUP_TABLE,"status = 1",'porder','desc',-1,0,true);
			foreach($module_group_list as &$module_group){
				$module_group_id = $module_group['id'];
				$module_group['menu_list'] = $this->model->fetch('*',ADMIN_MODULE_TABLE,"status = 1 AND parent_id = {$module_group_id}",'porder','desc',-1,0,true);
			}
			$data['module_group_list'] = $module_group_list;
			$view = 'menu_group';
		} else {
			$this->load->model('manager_modules/manager_modules_model');
			$data['menu'] = $this->manager_modules_model->list_module();
		}
		$this->load->view($view,$data);
	}
	
	function login(){
		if(!empty($_POST)){
			$pass = $this->input->post('pass');
			$user = $this->input->post('user');
			if(md5($pass)==$this->model->checkLogin($user)){
				if($user=='root'){
					$_SESSION['user_is_root'] = true;
				}
				$this->session->set_userdata('userInfo', $this->input->post('user'));
				print 1;
			} else {
				print 0;
			}
		} else {
			$this->load->view('BACKEND/login');
		}
	}
	
	function logout(){
		$this->session->unset_userdata('userInfo');
		unset($_SESSION['user_permission']);
		unset($_SESSION['user_is_root']);
		header('Location: '.PATH_URL.'admincp/login');
	}
	
	function permission(){
		$data['module'] = 'admincp';
		$this->template->write_view('content','permission',$data);
		$this->template->render();
	}
	
	function chk_perm($id_module,$type,$isAjax){
		$this->load->model('accounts/accounts_model');
		$this->load->model('admincp/admincp_model');
		$info = $this->admincp_model->getInfo($this->session->userdata('userInfo'));
		$pos = strpos($info[0]->permission,$id_module);
		$sub_str = substr($info[0]->permission,$pos,5);
		$pos_result = strpos($sub_str,$type);
		if($pos_result===false){
			if($isAjax==0){
				header('Location: '.PATH_URL.'admincp/permission');
				exit();
			}else{
				return 'permission-denied';
				exit;
			}
		}
	}
	
	function saveLog($func,$func_id,$field,$type,$old_value='',$new_value=''){
		/*if($field!=''){
			$data = array(
				'function' => $func,
				'function_id' => $func_id,
				'field' => $field,
				'type' => $type,
				'old_value' => $old_value,
				'new_value' => $new_value,
				'account' => $this->session->userdata('userInfo'),
				'ip' => getIP(),
				'created' => date('Y-m-d H:i:s')
			);
			$this->db->insert(ADMIN_LOGS_TABLE,$data);
		}else{
			foreach($new_value as $k=>$v){
				if($v!=$old_value->$k){
					$data = array(
						'function' => $func,
						'function_id' => $func_id,
						'field' => $k,
						'type' => $type,
						'old_value' => $old_value->$k,
						'new_value' => $v,
						'account' => $this->session->userdata('userInfo'),
						'ip' => getIP(),
						'created' => date('Y-m-d H:i:s')
					);
					$this->db->insert(ADMIN_LOGS_TABLE,$data);
				}
			}
		}*/
	}
	
	function update_profile(){
		if(!empty($_POST)){
			if(md5($this->input->post('oldpassAdmincp'))==$this->model->checkLogin($this->session->userdata('userInfo'))){
				$data = array(
					'username'=> $this->session->userdata('userInfo'),
					'password'=> md5($this->input->post('newpassAdmincp')),
				);
				$this->db->where('username', $this->session->userdata('userInfo'));
				if($this->db->update(ADMIN_USER_TABLE,$data)){
					print 'success_update_profile';
					exit;
				}
			}else{
				print 'error_update_profile';
				exit;
			}
		}else{
			$this->template->write_view('content','update_profile');
			$this->template->render();
		}
	}
}